﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.IO;
using System.Linq.Expressions;
using System.Reflection;
using Fuse8.ContentManagementFramework.SystemFacade.Common;

namespace Fuse8.ContentManagementFramework.BizFacade.Services.Contents.Queryable
{
    public class SupportedQueryableOperationsFacade
    {
        [ImportMany(typeof(ISupportedQueryableOperation))]
        protected List<ISupportedQueryableOperation> SupportedOperations = new List<ISupportedQueryableOperation>();

        private static IDictionary<string, ISupportedQueryableOperation> dictionary;
        private static object locker = new Object();

        private SupportedQueryableOperationsFacade()
        {
        }

        public static IDictionary<string, ISupportedQueryableOperation> Operations
        {
            get
            {
                if (dictionary != null)
                {
                    return dictionary;
                }

                var facade = new SupportedQueryableOperationsFacade();
                CmsExtensionManager.LoadExtensions(facade);

                lock (locker)
                {
                    if (dictionary != null)
                    {
                        return dictionary;
                    }

                    dictionary = new ConcurrentDictionary<string, ISupportedQueryableOperation>();
                    foreach (var operation in facade.SupportedOperations)
                    {
                        dictionary[operation.Name] = operation;
                    }
                }
                return dictionary;
            }
        }
    }
}